<?php
include_once "../controle/db.php";
include_once "../controle/valida.php";

$tipo = $_GET["tipo"];
$id = '';
$aluno = '';
$insti = '';
$msg = '';

if($_POST["btn_codebar"] == "ok"){
    
    if(valida::numero($_POST["codebar"])){
        $id = $_POST["codebar"];
    }
    
    if($id != ''){
        $sql = "select nome, instituicao from inscricao where id = '$id'";
        $con = db::conecta();
        $res = db::query($sql, $con);
        db::close($con);
        $c = db::fetch_array($res);
        if($c){
            $aluno = $c["nome"];
            $insti = $c["instituicao"];
        }
    }
}

if($_POST["btn_confirmar"] == "Confirmar"){
    
    if(valida::numero($_POST["id"])){$id = $_POST["id"];}
    if($tipo == "Entrada"){$io = "E";}
    if($tipo == "Saída"){$io = "S";}
    
    $sql = "
insert into transito(io, inscricao, dia, hora)
values ('$io', '$id', '".date("Y-m-d")."', '".date("H:i:s")."')";
    $con = db::conecta();
    $res = db::query($sql, $con);
    db::close($con);
    
    if($res){
        $msg = "Cadastrado com Sucesso";
        $id = '';
    }else{
        $msg = "Falha no cadastro";
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="shortcut icon" href="imagens/favicon.png">
        <link rel="stylesheet" href="css/style.css" type="text/css" media="all">
        <script type="text/javascript" src="js/javascript.js" ></script>
        <title>Controle de Entradas e Saidas - CONBRAD</title>
		<style>
				<?php
				if($_GET["tipo"] == "Entrada"){
				    echo "body,button {background:#E1FFE5} header,section,footer {background:#6FCE7F ";
				}else if($_GET["tipo"] == "Saída"){
				    echo "body,button {background:#FFE1E1} header,section,footer{background:#FF7676}";
				}else{
				    echo "body {background:99f} header,section,footer{background:#ccf}";
				}
				?>
		</style>
    </head>
    <body>
        <header>
            <div>
                <h2>Controle de Entradas e Saídas</h2>
                <h4>X CONBRAD</h4>  
            </div>
        </header>
        
        <section>
            <div id="formulario">
                <?php
                if($msg != ''){
                    echo "
                <script language='javascript'>
                    alert('$msg');
                </script>";
                }
                ?>
                <form method="post" action="formulario.php?tipo=<?=$tipo?>">
                    <span id="tipo"><?=$tipo?></span><br><br>
                    <label>ID do Inscrito</label><br>
                    <input type="text" size="30" name="codebar" id="codebar" value="<?=$id?>"><br>
                    <button type="submit" value="ok" name="btn_codebar"> OK </button>
                </form>
                <?php if($aluno != ''){?>
                <form method="post" action="formulario.php?tipo=<?=$tipo?>">
				<div id="mostra_dados">
		               <label id="nome"><?=$aluno?></label><br><br>
		               <label id="inst"><?=$insti?></label><br><br>
 				</div>
                    <input type="hidden" name="id" value="<?=$id?>">
                    <input type="hidden" name="aluno" value="<?=$aluno?>">
                    <input type="hidden" name="insti" value="<?=$insti?>">
                    <button type="submit" value="Confirmar" name="btn_confirmar" id="confirmar"> Confirmar </button>
                </form>
                <script language='javascript'>
                    foco("confirmar");
                </script>
                <?php }else{
                    echo "
                <script language='javascript'>
                    foco('codebar');
                </script>";
                } ?>
                <center><a href="index.php"><button id="voltar"> Voltar </button></a></center><br>
            </div>
        </section>
        
        <footer>
            <div>
                <label>AnguloWeb Soluções para Internet - Todos os Direitos Reservados</label>
            </div>
        </footer>

    </body>
</html>
